package com.ys.controller.api.finance;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwOperateCompanyPrepayment;
import com.ys.entity.sys.YsCol;
import com.ys.service.finance.TcwOperateCompanyPrepaymentService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 运营公司预付款管理
 */
@Controller
@RequestMapping("/api/inter/tcwOperateCompanyPrepayment")
public class ApiInterTcwOperateCompanyPrepaymentController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTcwOperateCompanyPrepaymentController.class);

    @Autowired
    private CommonApiService<TcwOperateCompanyPrepayment> commonApiService;

    @Autowired
    private TcwOperateCompanyPrepaymentService tcwOperateCompanyPrepaymentService;

    private Long menuId = MenuId.TCW_OperateCompanyPrepayment;


    //权限字符
    private final String PERMISSIONS_VIEW = "tcwOperateCompanyPrepayment:view";
    private final String PERMISSIONS_ADD = "tcwOperateCompanyPrepayment:add";

    private final String PERMISSIONS_EDIT = "tcwOperateCompanyPrepayment:edit";

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TcwOperateCompanyPrepayment setSeachSql(TcwOperateCompanyPrepayment cond){
        cond.addLeftJoin("THR_OperateCompanyApply C ON C.Vgsbm = A.Vgsbm ")
                .addField("C.Vgsjc AS vgsjc")//公司简称
                .addField("C.CUser AS cUser")//用户工号

        //排序
        .setOrderby("A.Vgsbm ASC");

        cond = commonApiService.dataAuth(menuId, cond);
        return  cond;
    }


    /**
     * 查询
     */
    @RequiresPermissions(PERMISSIONS_VIEW)
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TcwOperateCompanyPrepayment cond,String conditions
    ){
        try {
            cond = setSeachSql(cond);
            return commonApiService.searchLay(cond);
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }

    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TcwOperateCompanyPrepayment cond,String conditions){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond);
            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(menuId)
                    .setFrontShow(1)
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();

            //处理数据
            ExportExcelUtils<TcwOperateCompanyPrepayment> util = new ExportExcelUtils<>(new TcwOperateCompanyPrepayment());
            String toFilePath = util.exportExcel(listData, listField, "运营公司预付款管理");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }



    /**
     * 预付款退回申请调取运营公司预付款管理
     * @param cond
     * @param conditions
     * @return
     */
    @RequestMapping("/searchForModalyyYfksq")
    @ResponseBody
    public LayuiTableVoJA searchForModalyyYfksq(@ModelAttribute TcwOperateCompanyPrepayment cond, String conditions,String vyhzh,String vgsbm){
        cond.addField("A.Vid AS tkVid,A.Vyhzh AS tkVyhzh,A.Vkhh AS tkVkhh,A.Vyhzhmc AS tkVyhzhmc,A.preResAmount AS refundableAmount,A.id AS operateCompanyPrepaymentId")
                .setOrderby("A.vgsbm ASC")
                .where("A.PreResAmount != 0 AND A.PreResAmount IS NOT NULL")
                .addLeftJoin("THR_OperateCompanyBankInfo E ON E.vgsbm = A.vgsbm AND E.vyhzh = A.FkVyhzh")
                .addField("E.vlhh fkvlhh,E.id companyBankInfoId")
        .addLeftJoin("THR_OperateCompanyApply THRO ON  THRO.vgsbm = A.vgsbm")
                .addLeftJoin("TS_OperateOrg C on C.Id = THRO.OperateOrgId")
                .addField("C.Name operateOrgName")
                .addField("THRO.vgsjc AS vgsjc,THRO.vgsbm AS vgsbm")

                .addLeftJoin("TB_StaticConfig D ON D.Lev=THRO.OperationsAttribute AND D.KeyTwo = 'OperateCompanyApply-OperationsAttribute' ")
                .addField("D.NameSub operationsAttributeTxt")
                .setOperateCompanyPrepaymentType(0)
                .setStatus(1);
        return commonApiService.searchLay(cond);
    }




    /**
     * 新增
     */
    @RequiresPermissions(PERMISSIONS_ADD)
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toSave(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            //新增
            return tcwOperateCompanyPrepaymentService.add(params,menuId);
        } catch (Exception e) {
            logger.error("新增出错：",e.getMessage(),e);
            return R.error(e.getCause().getMessage());
        }
    }

    /**
     * 编辑
     */
    @RequiresPermissions(PERMISSIONS_EDIT)
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            //编辑
            return tcwOperateCompanyPrepaymentService.edit(params,menuId);
        } catch (Exception e) {
            logger.error("编辑出错：",e.getMessage(),e);
            return R.error(e.getCause().getMessage());
        }
    }

}